package com.salinity.kun.algorithm;

import com.salinity.kun.model.Point;

public class LinearEquation {

	private Point p1;
	private Point p2;
	private Double k;
	private Double b;

	public LinearEquation(Point p1, Point p2) {
		this.p1 = p1;
		this.p2 = p2;
	}

	public double getK() {
		
		//小数直接判断相等会出问题
		if (Math.abs(p1.getX() - p2.getX())<0.0000000001) {
			return 0.0;
		}
		if (k == null) {
			k = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
		}
		return k;
	}

	public double getB() {
		if (p1.getX() == p2.getX()){
			return 0.0;
		}
		if(b==null){
			b=p1.getY()-p1.getX()*getK();
		}
		return b;	
	}

	public double getY(double x){
		return getK()*x+getB();
	}
	
	public double getX(double y){
		if(getK()==0.0){
			return 0.0;
		}
		return (y-getB())/getK();
	}
}
